// pages/region/whInfo/index.js
var basePage = require('../../../common/BasePage')
Page({
    behaviors: [basePage],
    /**
     * 页面的初始数据
     */
    data: {
        phone: [],
        article_content: "",
        name: "",
        mobile: "",
        remark: "",
        showArea: false,
        addrs: [],
        reqData: {},
        adrr: "",
        showtype:false,
        actions: [{
                name: '城市仓',
                type: 2
            },
            {
                name: '区县仓',
                type: 1
            }
        ],
        typeInfo: {}
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.getArticle()
        this.getConfig()
        this.getAreas()
    },
    getAreas() {
        let that = this
        wx.request({
            url: that.data.host + "/uploads/gaode.json",
            header: {},
            success(res) {
                let result = res.data.districts[0].districts
                let arrs = [{
                    values: result
                }, {
                    values: result[0].districts,
                    defaultIndex: 0,
                }, {
                    values: result[0].districts[0].districts,
                    defaultIndex: 0,
                }]
                console.log("arrs", arrs)
                that.setData({
                    addrs: arrs
                })
            }
        })
    },
    // 省市区变动
    onAreaChange(e) {
        const {
            picker,
            value,
            index
        } = e.detail;
        if (index === 0) {
            // 修改省
            picker.setColumnValues(1, value[0].districts);
            picker.setColumnValues(2, value[0].districts[0].districts);
        } else if (index === 1) {
            // 修改市
            picker.setColumnValues(2, value[1].districts);
        }
    },
    // 确认选择省市区
    onAreaConfirm(e) {
        let areaArr = e.detail.value;
        let reqData = this.data.reqData;
        //初始化省市县数据
        reqData.province = areaArr[0].name;
        reqData.province_code = areaArr[0].adcode;
        reqData.city = areaArr[1].name;
        reqData.city_code = areaArr[1].adcode;
        reqData.district = areaArr[2].name;
        reqData.district_code = areaArr[2].adcode;
        this.setData({
            addr: reqData.province + reqData.city + reqData.district,
            showArea: false,

        })

    },
    //获取文章
    getArticle() {
        this.request({
            url: this.data.host + '/home/article/detail?article_id=106'
        }, this.get_detail_ok);
    },
    get_detail_ok(res) {
        console.log("get_detail_ok", res);
        this.setData({ 
            article_content: res.data.article_content.content.replace(/<img /ig, '<img width="100%" ')
            .replace(/<img /ig, '<img show-menu-by-longpress ')
        });
    },
    //配置信息
    getConfig() {
        this.request({
            url: this.data.host + '/home/whRecruitMsg'
        }, res => {
            this.setData({
                phone: res.data.tel
            })
        });
    },
    onPhone(e) {
        var phone =  e.currentTarget.dataset.item
        wx.makePhoneCall({
            phoneNumber: phone
        })
    },
    submit() {
        var {
            name,
            mobile,
            remark,
            reqData,
            addr,
            typeInfo
        } = this.data
        if (!name) {
            wx.showToast({
                title: '请输入您的姓名',
                icon: "none"
            })
            return;
        }
        if (!mobile) {
            wx.showToast({
                title: '请输入您的电话',
                icon: "none"
            })
            return;
        }
        if (!typeInfo.name) {
            wx.showToast({
                title: '请选择入驻类型',
                icon: "none"
            })
            return;
        }
        if (!addr) {
            wx.showToast({
                title: '请选择入驻地区',
                icon: "none"
            })
            return;
        }
        this.setData({
            showInfo: false
        })
        this.request({
            url: this.data.host + "/home/whRecruit",
            data: {
                name,
                mobile,
                remark,
                type:typeInfo.type,
                ...reqData
            },
            method: "POST",
            loadingTip: "提交中···"
        }, res => {
            wx.showToast({
              title: res.msg,
            })
        })
    },
    selectType(e) {
        this.setData({
            typeInfo:e.detail,
            showtype:false

        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})